//
// Created by HP on 2021/12/26.
//

#ifndef SPH_SPH_SYSTEM_H
#define SPH_SPH_SYSTEM_H

#include "stdio.h"
#include "sph_math.h"
#include "thread"
#include "vector"

#define NORMAL 0x0c
#define WALL   0x0d

struct Particle {
    Vec2 pos;
    Vec2 v;  // 速度
    double dens;  // 密度
    int type;  // 类型: 流体和刚体, 但刚体模拟得不成功, 所以这一项暂时没用
};

struct Grid {
    std::vector<int> *grid;
    int h;
    int w;
};

class SPHSystem {
public:
    SPHSystem(int width, int height, double u, double m, double K, double dens_0, double h, double grid_scale);
    void   update();
    void   flush() const {
        int height = grid.h;
        for (int i = 0; i <= height; i++) {
            printf("\033[1A");
        }
    }
    void   add_particle(Particle p) {
        particles.push_back(p);
    }
    double calculate_density(const Particle& pi, int id);
    Vec2   calculate_a_pressure(const Particle& pi, int id);
    Vec2   calculate_a_viscosity(const Particle& pi, int id);
    void   render() const;
private:
    std::vector<Particle> particles;
    Grid grid{};
    char   *cache;
    double u;
    double m;
    double K;
    double dens_0;
    double h;
    double grid_scale;

    double h2;
    double h4;
    double h5;
    double h8;
    double K_poly6;
    double K_spiky;
    double K_viscosity;
};

#endif //SPH_SPH_SYSTEM_H
